# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Project  :pythonProject
# @File     :smokefire
# @Date     :2021/9/23 11:48
# @Author   :zhumenglong
# @Software :PyCharm
-------------------------------------------------
"""
from wareHouse import *

class Fire:
    def __init__(self,file_path,Index_path,goal_path):
        self.file_path = file_path
        self.Index_path = Index_path
        self.goal_path = goal_path

    def get_keypoint(self):
        with open(self.Index_path, 'r', encoding='utf-8') as fp:
            while True:
                file_name = fp.readline()[:-1]
                json_name = ""
                pict_name = ""
                if len(file_name) > 0:
                    json_name = self.file_path + file_name + '_info.json'
                    pict_name = self.file_path + file_name + '.jpg'
                    Fire.draw_pic_jpg(self, file_name, json_name, pict_name)

                else:
                    break

    def draw_pic_jpg(self, file_name, json_name, pict_name):
        with open(json_name, 'r', encoding='utf-8') as f:
            json_info = json.loads(f.read())
            img_info = method.cv2_imread(pict_name, -1)
            for i in json_info:
                if i == "box":
                    for num in json_info[i]:
                        # print(num)
                        for j in num:
                            # print(j)
                            cat_name = "fire"
                            xmin = int(num['x'])
                            ymin = int(num['y'])
                            xmax = int(num['x']) + int(num['width'])
                            ymax = int(num['y']) + int(num['height'])
                            color = (250, 0, 0)
                            cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color,
                                          2)
                            font = cv2.FONT_HERSHEY_SIMPLEX
                            cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, color, 2)
                            cv2.putText(img_info, "width:" + str(int(num['width'])), (int(xmin) + 100, int(ymin)),
                                        cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
                            cv2.putText(img_info, "height:" + str(int(num['height'])), (int(xmin) + 100, int(ymin)+40),
                                        cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)

                elif i == "detect_type":
                    cv2.putText(img_info, json_info[i], (1600, 300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 250, 0), 3)

            method.create_path(self.goal_path)

            method.cv2_imwrite_jpg("./" + self.goal_path + "/" + file_name + ".jpg", img_info)